package net.hn.hnms.biz.upkafka.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import net.hn.hnms.biz.upkafka.config.SpringContextUtil;
import net.hn.hnms.biz.upkafka.domain.CoalflowNowPo;
import net.hn.hnms.biz.upkafka.domain.FanrealNowPo;
import net.hn.hnms.biz.upkafka.domain.InclineHoistingNowPo;
import net.hn.hnms.biz.upkafka.domain.SynergiaNowPo;
import net.hn.hnms.biz.upkafka.mapper.CoalflowNowMapper;
import net.hn.hnms.biz.upkafka.mapper.FanrealNowMapper;
import net.hn.hnms.biz.upkafka.mapper.InclineHoistingNowMapper;
import net.hn.hnms.biz.upkafka.mapper.SynergiaNowMapper;
import net.hn.hnms.biz.upkafka.service.SystemService;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 获取个系统的状态
 */
@Service
public class SystemServiceImpl  implements SystemService {

    /**
     * 获取综采系统采煤机运行状态
     */
//    @Resource
//    private SynergiaNowMapper synergiaNowMapper;


//    @Async
    @Override
    public SynergiaNowPo getZCSystemStatus(String mineCode) {

//        // 手动从 Spring 容器中获取 systemService bean
//        SynergiaNowMapper synergiaNowMapper = applicationContext.getBean(SynergiaNowMapper.class);
        SynergiaNowMapper bean = SpringContextUtil.getBean(SynergiaNowMapper.class);
        SynergiaNowPo synergiaNowPo = bean.selectOne(new LambdaQueryWrapper<SynergiaNowPo>()
                .eq(SynergiaNowPo::getPointCode, "NewSite_采煤_煤机方向")
                .eq(SynergiaNowPo::getMineCode, mineCode));
        if (synergiaNowPo != null) {
            //（-1：煤机向架号减小方向。运行，0：煤机未运行，1：煤机向架号增加方向运行
            synergiaNowPo.setPointValue(Float.valueOf(synergiaNowPo.getPointValue() != null && synergiaNowPo.getPointValue().intValue() > 0 ? 1 : 0));
            return synergiaNowPo;
        } else {
            SynergiaNowPo synergiaNowPo1 = new SynergiaNowPo();
            synergiaNowPo1.setPointValue(Float.valueOf(0));
            synergiaNowPo1.setPointStatus(1);
            synergiaNowPo1.setDataTime(new Date());
            synergiaNowPo1.setMineCode(mineCode);
            return synergiaNowPo1;
        }
    }
    @Override
    public Map<String, SynergiaNowPo> getZCSystem(String mineCode) {

//        // 手动从 Spring 容器中获取 systemService bean
//        SynergiaNowMapper synergiaNowMapper = applicationContext.getBean(SynergiaNowMapper.class);
        SynergiaNowMapper bean = SpringContextUtil.getBean(SynergiaNowMapper.class);
        LambdaQueryWrapper<SynergiaNowPo> wrapper = new LambdaQueryWrapper<SynergiaNowPo>();
        if ("150621011015".equals(mineCode)) {
            //高头窑的煤矿编码需要特殊处理
            mineCode = "150621011038";
        }
        wrapper.eq(SynergiaNowPo::getMineCode, mineCode);
        List<SynergiaNowPo> synergiaNowPos = bean.selectList(wrapper);
        if (synergiaNowPos != null) {
            //根据pointcode转成map 重复的数据获取第一个
            Map<String, SynergiaNowPo> fanrealNowPoMap = synergiaNowPos.stream()
                    .filter(p -> p.getPointCode() != null)
                    .collect(Collectors.toMap(SynergiaNowPo::getPointCode, p -> Optional.ofNullable(p).orElse(new SynergiaNowPo()), (existing, replacement) -> replacement));
            return fanrealNowPoMap;
        } else {
            return new HashMap<>();
        }
    }
    @Override
    public Map<String, FanrealNowPo> getZtfSystemStatus(String mineCode) {
        if ("150621011015".equals(mineCode)) {
            //高头窑的煤矿编码需要特殊处理
            mineCode = "150621011038";
        }
//        // 手动从 Spring 容器中获取 systemService bean
//        SynergiaNowMapper synergiaNowMapper = applicationContext.getBean(SynergiaNowMapper.class);
        FanrealNowMapper bean = SpringContextUtil.getBean(FanrealNowMapper.class);
        List<FanrealNowPo> fanrealNowPos = bean.selectList(new LambdaQueryWrapper<FanrealNowPo>().eq(FanrealNowPo::getMineCode, mineCode));
        if (fanrealNowPos != null) {
            //根据pointcode转成map 重复的数据获取第一个
            Map<String, FanrealNowPo> fanrealNowPoMap = fanrealNowPos.stream()
                    .filter(p -> p.getPointCode() != null)
                    .collect(Collectors.toMap(FanrealNowPo::getPointCode, p -> Optional.ofNullable(p).orElse(new FanrealNowPo()), (existing, replacement) -> replacement));
            return fanrealNowPoMap;
        } else {
            return new HashMap<>();
        }

    }

    @Override
    public Map<String, InclineHoistingNowPo> getXJSSSystemStatus(String mineCode) {
        if ("150621011015".equals(mineCode)) {
            //高头窑的煤矿编码需要特殊处理
            mineCode = "150621011038";
        }
        InclineHoistingNowMapper bean = SpringContextUtil.getBean(InclineHoistingNowMapper.class);
        List<InclineHoistingNowPo> inclineHoistingNowPos = bean.selectList(new LambdaQueryWrapper<InclineHoistingNowPo>().eq(InclineHoistingNowPo::getMineCode, mineCode));
        if (inclineHoistingNowPos != null) {
            //根据pointcode转成map 重复的数据获取第一个
            Map<String, InclineHoistingNowPo> inclineHoistingNowPoMap = inclineHoistingNowPos.stream()
                    .filter(p -> p.getPointCode() != null)
                    .collect(Collectors.toMap(InclineHoistingNowPo::getPointCode, p -> Optional.ofNullable(p).orElse(new InclineHoistingNowPo()), (existing, replacement) -> replacement));
            return inclineHoistingNowPoMap;
        } else {
            return new HashMap<>();
        }
    }

    /**
     * 皮带实时数据
     * @param mineCode
     * @return
     */
    @Override
    public Map<String, CoalflowNowPo> getPdSSystemStatus(String mineCode) {
        if ("150621011015".equals(mineCode)) {
            //高头窑的煤矿编码需要特殊处理
            mineCode = "150621011038";
        }
        CoalflowNowMapper bean = SpringContextUtil.getBean(CoalflowNowMapper.class);
        List<CoalflowNowPo> coalflowNowPos = bean.selectList(new LambdaQueryWrapper<CoalflowNowPo>().eq(CoalflowNowPo::getMineCode, mineCode));
        if (coalflowNowPos != null) {
            //根据pointcode转成map 重复的数据获取第一个
            Map<String, CoalflowNowPo> coalineHoistingNowPoMap = coalflowNowPos.stream()
                    .filter(p -> p.getPointCode() != null)
                    .collect(Collectors.toMap(CoalflowNowPo::getPointCode, p -> Optional.ofNullable(p).orElse(new CoalflowNowPo()), (existing, replacement) -> replacement));
            return coalineHoistingNowPoMap;
        } else {
            return new HashMap<>();
        }
    }

}
